﻿//---------------------------------------------   Framework   -------------------------------------

var Ue;
if (!Ue) {
    Ue = {};
}

ue.framework = {
    //对像类型判断
    //例：ue.framework.Is("abc", "String")
    is: function (obj, type) {
        var toString = Object.prototype.toString, undefined;
        return (type === "Null" && obj === null) ||
              (type === "Undefined" && obj === undefined) ||
              toString.call(obj).slice(8, -1) === type;
    },
    //深拷贝
    deepCopy: function (source) {
        var result = source.constructor == Object
            ? new this.constructor()
            : new this.constructor(source.valueOf());

        for (var key in source) {
            var copy = source[key];
            if (result === copy) continue; //如window.window === window，会陷入死循环，需要处理一下
            if (ue.framework.is(copy, "Object")) {
                result[key] = arguments.callee(result[key] || {}, copy);
            } else if (ue.framework.is(copy, "Array")) {
                result[key] = arguments.callee(result[key] || [], copy);
            } else {
                result[key] = copy;
            }
        }
        return result;
    },
    htmlEncode: function (str) {
        var s = "";

        if (str.length == 0) return "";

        s = str.replace(/&/g, "&amp;");

        s = s.replace(/</g, "&lt;");

        s = s.replace(/>/g, "&gt;");

        s = s.replace(/\'/g, "&#39;");

        s = s.replace(/\"/g, "&quot;");

        return s;
    },
    htmlDecode: function (str) {

        var s = "";

        if (str.length == 0) return "";

        s = str.replace(/&amp;/g, "&");

        s = s.replace(/&lt;/g, "<");

        s = s.replace(/&gt;/g, ">");

        s = s.replace(/&#39;/g, "\'");

        s = s.replace(/&quot;/g, "\"");

        return s;
    },
    timeSpan: function (startTime, endTime) {
        var self=this;
        self.beginDateTime=new Date(startTime);
        self.endDateTime=new Date(endTime);

        self.totalMilliseconds  =Math.abs(self.endDateTime.getTime() - self.beginDateTime.getTime());

        self.day=function(){
            return self.totalDay();
        };

        self.hour=function(){
            return self.totalHour(self.totalMilliseconds % (1000 * 60 * 60 * 24));           
        };

        self.minute=function(){
            return self.totalMinute(self.totalMilliseconds % (1000 * 60 * 60));                  
        };

        self.second=function(){
            return self.totalSecond(self.totalMilliseconds % (1000 * 60)); 
        };

        self.totalDay=function(totalMilliseconds){ 
            return self.calculateByDivision(totalMilliseconds,1000 * 60 * 60 * 24);     
        };

        self.totalHour=function(totalMilliseconds){
            return self.calculateByDivision(totalMilliseconds,1000 * 60 * 60);    
        };

        self.totalMinute=function(totalMilliseconds){
            return self.calculateByDivision(totalMilliseconds,1000 * 60);    
        };

        self.totalSecond=function(totalMilliseconds){  
            return self.calculateByDivision(totalMilliseconds,1000);                 
        };

        self.calculateByDivision =function(left,right){
            if(!left)
            {
                left=self.totalMilliseconds;
            }

            return Math.floor(left / right);
        };
    }
};

///转换
ue.framework.convert = {
    toInt32: function (strNum) {
        var label = parseInt(strNum);
        if (isNaN(label))
            label = 0;
        return label;
    },

    toBoolean: function (strBool) {
        if (strBool.toString().toLowerCase() == "true" || strBool.toString().toLowerCase() == "1")
            return true;
        else
            return false;
    },
    toDateTime: function (longDateTime, format) {
        !format && (format = 'yyyy-MM-dd hh:ss');

        if (typeof (longDateTime) === 'string') {
            return eval('new ' + (longDateTime.replace(/\//g, ''))).format(format);
        }
        else {
            return new Date((longDateTime || 0) * 1000).format(format);
        }
    }
};

ue.framework.request = {
    //取获参数值
    getQueryValue: function (queryName, url) {
        if (!url) {
            url = window.location.search.substr(1);
        }
        var reg = new RegExp("(^|&)" + queryName + "=([^&]*)(&|$)");
        var r = url.match(reg);

        return r != null
            ? decodeURIComponent(r[2])
            : null;
    },
    setQueryValue: function (queryName, value, url) {
        if (!url) {
            url = window.location.href;
        }

        url = url.toLowerCase();
        queryName = queryName.toLowerCase();

        var reg = new RegExp("(&)?" + queryName + "=([^&]*)");
        url = url.replace(reg, "");

        var split = "?";
        if (url.indexOf(split) >= 0) {
            if (url.substring((url.length - 1)) == '?') {
                split = "";
            }
            else {
                split = "&";
            }
        }
        return url + split + queryName + "=" + encodeURIComponent(value);
    }
};

ue.framework.response = {
    redirect: function (url) {
        var a = $("<a href='" + encodeURI(url) + "' style='display:none;'></a>");
        //在一些浏览器中只有把element加入到页面中，它的click事件才会生效。如：ie6
        $("body").append(a);

        if (!$.browser.msie) {
            var e = document.createEvent('MouseEvent');
            e.initEvent('click', false, false);
            a[0].dispatchEvent(e);
        }

        a[0].click();
    }
};


//字符串，添加format方法 使用方法 var a="1213{0},fdsfdsf{1}";a.format("1","2");
String.prototype.format = function () {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g,
        function (m, i) { return args[i]; }
    );
}
String.prototype.endsWith = function (suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
String.prototype.startsWith = function (suffix) {
    return this.indexOf(suffix) == 0;
};
String.prototype.trim = function () {
    return !this
            ? this
            : this.replace(/^(&nbsp;|\s)*|(&nbsp;|\s)*$/gi, '');
};

//时间格式化
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1,  //month
        "d+": this.getDate(),       //day
        "h+": this.getHours(),      //hour
        "m+": this.getMinutes(),    //minute
        "s+": this.getSeconds(),    //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}
